<?php


namespace App\Http\Controllers\App\V1;

use Carbon\Carbon;
use App\Services\Tools\RedisService;
use Illuminate\Http\Request;

//引用http请求类
use App\Exceptions\BasicException;

//引用DB
use App\Response\Response;
class ExternalRequest
{
    public function userFollowNew(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'user_id' => 'required|numeric',
                'user_id_zj' => 'required|numeric',
                'type' => 'filled|in:1,2',        //关注接口的类别1是关注2是取消关注
            ]
        );

        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $user_id = $request->input('user_id', 0);
        $user_id_zj = $request->input('user_id_zj', 0);

        $type = $request->input('type', 1);

        if ($user_id == $user_id_zj) {
            throw new BasicException(50003);
        }

        $count = app('model_user')->whereIn('user_id', [$user_id_zj, $user_id])->count();

        if ($count != 2) {
            throw new BasicException(50001);
        }

        $follow_record = app('model_follow')->where(['user_id' => $user_id_zj, 'followed_user' => $user_id])->first();


        //进行业务拆解
        if ($type == 1) {
            if ($follow_record) {
                throw new BasicException(50002);
            }

            //添加关注关系
            $follow_data = [
                'user_id' => $user_id_zj,
                'followed_user' => $user_id,
                'add_time' => time(),
            ];

            $res = app('model_follow')->create($follow_data);
            if (!$res) {
                throw new BasicException(50007);
            }

            //------发送关注消息-----
            //检查有没有此人的关注消息,有不重复发送
            $message_detail = app('model_microblog_message')->where([
                'user_id' => $user_id,
                'uid' => $user_id_zj,
                'type' => 3,
            ])->first();

            if (empty($message_detail)) {
                $message_data = [
                    'user_id' => $user_id,
                    'uid' => $user_id_zj,
                    'type' => 3,
                    'add_time' => time(),
                ];
                app('repo_user')->createMicroMessage($message_data);
            }

            //粉丝通知部分
            $user_config = app('model_user_config')->where('user_id', $user_id)->select('fans')->first();
            if (isset($user_config->fans) && ($user_config->fans == 1)) {
                //开启了粉丝通知,发送消息
                $nickname = app('model_user_data')->where('user_id', $user_id_zj)->select('nickname')->first();
                $nickname = !empty($nickname) ? $nickname->nickname : '';
                $content = "您有新的粉丝" . $nickname . "关注了你";
                app('model_user_news')->create(['user_id' => $user_id, 'title' => '关注消息', 'content' => $content,
                    'add_time' => Carbon::now()->timestamp,
                ]);
                app('redis')->del('user:news:countStat:' . $user_id);
                app('redis')->del('user:news:infoList:' . $user_id);
            }
        } else {
            if (empty($follow_record)) {
                throw new BasicException(50009);
            }

            //解除关注关系
            $res = app('model_follow')->where(['user_id' => $user_id_zj, 'followed_user' => $user_id])->delete();
            if (!$res) {
                throw new BasicException(50010);
            }
        }

        //查询是否相互关注
        $follow_other = app('model_follow')->where(['user_id' => $user_id, 'followed_user' => $user_id_zj])->first();

        if (!empty($follow_other)) {
            if ($type == 1) {
                //关注
                //创建好友关系
                app('model_friend')->create(['user_id' => $user_id, 'friend_id' => $user_id_zj, 'add_time' => time()]);
                app('model_friend')->create(['user_id' => $user_id_zj, 'friend_id' => $user_id, 'add_time' => time()]);
                app('rongcloud')->whitelistADD($user_id_zj, [$user_id]);
                app('rongcloud')->whitelistADD($user_id, [$user_id_zj]);
                RedisService::addWhitelist($user_id, $user_id_zj);
                RedisService::addWhitelist($user_id_zj, $user_id);
                RedisService::addFriend($user_id, $user_id_zj);
            } else {
                throw new BasicException(50010);
//                //取消关注
//                app('model_friend')->where(['user_id' => $user_id, 'friend_id' => $user_id_zj])->delete();
//                app('model_friend')->where(['user_id' => $user_id_zj, 'friend_id' => $user_id])->delete();
//                RedisService::delFriend($user_id, $user_id_zj);
//                $sociaty_user_id = SociatyUser::query()->where(['user_id' => $user_id_zj, 'is_speak' => 2])->value('id');
//                $sociaty_to_user_id = SociatyUser::query()->where(['user_id' => $user_id, 'is_speak' => 2])->value('id');
//                if (empty($sociaty_user_id) && empty($sociaty_to_user_id)) {
//                    app('rongcloud')->whitelist($user_id_zj, [$user_id]);
//                    app('rongcloud')->whitelist($user_id, [$user_id_zj]);
//                    RedisService::delWhitelist($user_id, $user_id_zj);
//                    RedisService::delWhitelist($user_id_zj, $user_id);
//                }

            }
        }

        //清除redis的相关记录
        app('redis')->del('user:friend:count:' . $user_id_zj);
        app('redis')->del('user:friend:count:' . $user_id);
        app('redis')->del('user:fans:count:' . $user_id_zj);
        app('redis')->del('user:follow:count:' . $user_id_zj);
        app('redis')->del('user:fans:count:' . $user_id);
        app('redis')->del('user:follow:count:' . $user_id);

        return Response::sendData(true);
    }

}
